package com.fingard.dsp.bank.directbank.icbc03;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class ICBC03DeContract extends ICBC03Base implements IDeContract {
    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "contract");
            if (actItem == null) {
                throw new Exception("未找到账号" + p_req.contractDetail.recAct + "对应的商户配置信息");
            }
            XmlTextWriter xmlWriter = new XmlTextWriter();
            JSONObject data = new JSONObject();
            data.put("app_id",actItem.appId);
            txCode = "ECIP000179";
            reqSeqID = p_req.contractDetail.askNumber;
            writeCommonHeader(xmlWriter);
            JSONObject bizContent = new JSONObject();
            bizContent.put("consumer_id","baoxian");
            bizContent.put("merchant_id",actItem.merchantId);
            bizContent.put("agreement_no",p_req.contractDetail.authCode);
            bizContent.put("merchant_type","1");
            data.put("bizContent",bizContent);
            xmlWriter.writeElementString("Transaction_Body",JSONObject.toJSONString(data));
            xmlWriter.writeEndDocument();//Transaction_Body
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String returnMsg = tmpBkReader.getFirstText("/TX/TX_BODY/ENTITY/Transaction_Body");
                JSONObject retObj = JSONObject.parseObject(returnMsg);
                tmpRetDetail.respCode = "0";
                if("0".equals(retObj.getString("returnCode")) || returnMsg.contains("商户上送订单信息有误，未查询到该小额免密协议")){
                    tmpRetDetail.deSignState = ContractState.SUCCESS;
                }else {
                    tmpRetDetail.respCode = "-1";
                }
                tmpRetDetail.respInfo = retObj.getString("returnMsg");
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpSbMsg.toString();
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        return null;
    }
}
